/*
 * @Description: olcs 删除操作
 * @Date: 2022-03-31 15:31:32
 * @Author: yeming
 */

/**
 * @desc: 清除地图上对应的Feature，同时删除对应hash Map中指定项
 * @param {Number} sessionId 操作对象的sessionId
 * @param {Object} hashMap 操作对象的hash Map
 * @param {Object} vectorSource 操作对象的矢量图层数据源
 * @param {Object} entities 3d entities
 */
export function removeFeatureAndDeleteHashMap (sessionId, hashMap, vectorSource, entities) {
  let currentHashMap = hashMap.get(sessionId)
  if (!currentHashMap) {
    console.error("currentHashMap null or undefined")
    return false
  }

  let geom2d = currentHashMap.item2d
  if (!geom2d) {
    console.error("geom2d undefined")
    return false
  }
  let feature = geom2d.feature
  if (!feature) {
    console.error("feature undefined")
    return false
  }
  vectorSource.removeFeature(feature)
  hashMap.delete(sessionId)

  if (!entities) return false
  let geom3d = currentHashMap.item3d
  if (geom3d) entities.remove(geom3d.entity)
}

/**
 * @desc: 删除所有Feature以及hash Map
 * @param {Object} hashMap 操作对象的hash Map
 * @param {Object} vectorSource 操作对象的矢量图层数据源
 * @param {Object} entities 3d entities
 */
export function removeAllFeatureAndDeleteHashMap (hashMap, vectorSource, entities) {
  for (let value of hashMap.values()) {
    let geom2d = value.item2d
    let feature = geom2d.feature
    if (!feature) {
      console.error("feature undefined")
      continue
    }
    vectorSource.removeFeature(feature)

    if (!entities) continue
    let geom3d = value.item3d
    if (geom3d) entities.remove(geom3d.entity)
  }
  hashMap.clear()
}